//
//  LDXNavViewController.swift
//  微博
//
//  Created by 吕东星 on 17/3/29.
//  Copyright © 2017年 lddongxing. All rights reserved.
//

import UIKit

class LDXNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

     self.interactivePopGestureRecognizer?.delegate = self
    }
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
//        print("123456")
        if self.viewControllers.count > 0 {
            //  如果当前的控制的个数等于1,表示根视图控制器
            if self.viewControllers.count == 1 {
                //  获取上一级控制器的标题
                let title = self.viewControllers.first?.title
                //  设置push进来控制器的左侧按钮
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!,imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                //  设置视图控制的标题
                viewController.navigationItem.title = "当前控制器显示的是第\(self.viewControllers.count + 1)级控制"
            } else {
                //  不是第二级控制器的导航栏按钮的设置
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回",imageName: "navigationbar_back_withtext", target: self, action: #selector(leftAction))
                //  设置视图控制的标题
                viewController.navigationItem.title = "当前控制器显示的是第\(self.viewControllers.count + 1)级控制"
            }
            //  隐藏tabbar,会有黑影
            //  1. 解决设置window背景色
            //  2. 设置tabbar视图的背景图片
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        
        
//        print("当前控制器的个数\(self.viewControllers.count)")
        super.pushViewController(viewController, animated: animated)
//        print("当前控制器的个数\(self.viewControllers.count)")
    }
    
    //  MARK: ---左侧导航栏按钮的点击
    @objc private func leftAction() {
        //  返回
        self.popViewController(animated: true)
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool{
        if self.viewControllers.count == 1 {
            return false
        }
       return true
    }

}
